﻿using System;
using System.Collections.Generic;
using System.Text;
using Google.GData.Client;

namespace SpacesToBloggerMigrator
{
    class BloggerEngine : IBlogEngine
    {
        #region IBlogEngine Members

        private Service proxy;
        private Uri blog;
        public void Login(string username, string password, Uri blogURL)
        {
            this.proxy = new Service("blogger", "blogger-example");
            this.proxy.Credentials = new GDataCredentials(username, password);
            this.blog = GetUserBlog(blogURL);
        }

        public List<_Post> GetPosts()
        {
            List<_Post> result = new List<_Post>();
            FeedQuery query = new FeedQuery();
            query.Uri = this.blog;
            AtomFeed feed = this.proxy.Query(query);
            while (true) 
            {
                foreach (AtomEntry entry in feed.Entries)
                {
                    _Post p = new _Post();
                    p.Title = entry.Title.Text;
                    p.Date = entry.Published;
                    p.Content = entry.Content.Content;
                    result.Add(p);
                }
                if (string.IsNullOrEmpty(feed.NextChunk))
                    break;
                query.Uri = new Uri(feed.NextChunk);
                feed = this.proxy.Query(query);
            }
            return result;
        }

        public bool InsertPost(_Post post)
        {
            AtomEntry createdEntry = null;
            AtomEntry newPost = new AtomEntry();
            newPost.Title.Text = post.Title;
            newPost.Content = new AtomContent();
            newPost.Content.Content = post.Content;
            newPost.Content.Type = "xhtml";
            newPost.Published = post.Date;
            createdEntry = this.proxy.Insert(this.blog, newPost);
            if (createdEntry != null)
                return true;
            else
                return false;
        }

        #endregion

        internal Uri GetUserBlog(Uri blog)
        {
            FeedQuery query = new FeedQuery();
            query.Uri = new Uri("http://www.blogger.com/feeds/default/blogs");
            AtomFeed feed = this.proxy.Query(query);
            Uri blogPostUri = null;
            if (feed != null)
            {
                foreach (AtomEntry entry in feed.Entries)
                {
                    if (entry.AlternateUri.Content.Equals(blog.AbsoluteUri))
                        for (int i = 0; i < entry.Links.Count; i++)
                            if (entry.Links[i].Rel.Equals("http://schemas.google.com/g/2005#post"))
                            {
                                blogPostUri = new Uri(entry.Links[i].HRef.ToString());
                                break;
                            }
                        return blogPostUri;
                }
            }
            return blogPostUri;
        }
    }
}
